/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.center.web.action.event;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;

import com.sinodata.bsm.center.cache.EventRuleCache;
import com.sinodata.bsm.center.cache.EventTypeCache;
import com.sinodata.bsm.center.purview.FunctionConstants;
import com.sinodata.bsm.center.purview.UserFunctionValidate;
import com.sinodata.bsm.center.service.event.EventTypeService;
import com.sinodata.bsm.center.util.web.Struts2Utils;
import com.sinodata.bsm.center.web.action.BaseAction;
import com.sinodata.bsm.common.constants.EventConstants;
import com.sinodata.bsm.common.vo.EventRule;
import com.sinodata.bsm.common.vo.EventType;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author dongyushi
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-7-18 上午9:14:19          dongyushi        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class EventTypeAction extends BaseAction<EventType> {

    /**
     * 
     */
    private static final long serialVersionUID = -126058851654657719L;

    @Autowired
    private EventTypeService eventTypeService;

    @Autowired
    private EventTypeCache eventTypeCache;

    @Autowired
    private EventRuleCache eventRuleCache;

    private EventType entity;

    private Long id;

    public String common() {
        request.setAttribute("resId", request.getParameter("id"));
        return "common";
    }

    @Override
    public EventType getModel() {
        return entity;
    }

    @Override
    public String list() throws Exception {
        String resId = request.getParameter("resId");
        List<EventType> list = eventTypeService.findByResId(Long.valueOf(resId));
        JSONObject json = new JSONObject();
        json.element("iTotalRecords", list == null ? 0 : list.size());
        json.element("iTotalDisplayRecords", list == null ? 0 : list.size());
        JSONArray array = new JSONArray();
        for (int i = 0; list != null && i < list.size(); i++) {
            EventType eventType = list.get(i);
            JSONObject obj = new JSONObject();
            obj.element("op", UserFunctionValidate.resFunctionValidate(Long.valueOf(resId), FunctionConstants.EDIT_RES) ? "<div style='width:50px; padding-left:22px;'><a href='javascript:update(" + eventType.getId() + ");'><img src='../style/default/bsm/images/edit.gif' title='修改'></a>&nbsp;&nbsp;<a href='javascript:del(" + eventType.getId() + ");'><img src='../style/default/bsm/images/delete.gif' title='删除'></a><div>" : "");
            obj.element("id", eventType.getId());
            obj.element("name", eventType.getName());
            obj.element("recoverable", eventType.getRecoverable() == EventConstants.TYPE_RECOVERABLE ? "可恢复" : "不可恢复");
            array.add(obj);
        }
        json.element("aaData", array);
        renderText(json.toString());
        return null;
    }

    /*
     * 转向新增页面
     */
    public String add() throws Exception {
        String resId = request.getParameter("resId");
        Struts2Utils.getRequest().setAttribute("resId", resId);
        return "add";
    }

    @Override
    public String input() throws Exception {
        EventType eventType = eventTypeCache.get(Long.valueOf(request.getParameter("id")));
        Struts2Utils.getRequest().setAttribute("eventType", eventType);
        return "update";
    }

    public String checkCode() {
        String oldCode = request.getParameter("oldCode");
        if (oldCode != null) {
            if (oldCode.equals(request.getParameter("code"))) {
                renderText("true");
                return null;
            }
        }
        List<EventType> list = eventTypeCache.findByResId(Long.valueOf(request.getParameter("resId")));
        if (list == null) {
            renderText("true");
            return null;
        }
        for (EventType eventType : list) {
            if (eventType.getCode().equals(request.getParameter("code"))) {
                renderText("false");
                return null;
            }
        }
        renderText("true");
        return null;
    }

    public String checkName() throws Exception {
        String name = new String(request.getParameter("name").getBytes("ISO-8859-1"), "UTF-8");
        if (request.getParameter("oldName") != null) {
            String oldName = request.getParameter("oldName");
            if (oldName.equals(name)) {
                renderText("true");
                return null;
            }
        }
        List<EventType> list = eventTypeCache.getAll();
        for (EventType eventType : list) {
            if (eventType.getName().equals(new String(request.getParameter("name").getBytes("ISO-8859-1"), "UTF-8"))) {
                renderText("false");
                return null;
            }
        }
        renderText("true");
        return null;
    }

    @Override
    public String save() throws Exception {
        if (entity.getId() != null) {//修改
            if (eventTypeCache.get(entity.getId()) != null) {
                eventTypeService.update(entity);
                eventTypeCache.put(entity.getId(), entity);
            } else {
                renderText("false");
                return null;
            }
        } else {//新增
            eventTypeService.add(entity);
            eventTypeCache.put(entity.getId(), entity);
        }
        renderText("true");
        return null;
    }

    @Override
    public String delete() throws Exception {
        List<EventRule> list = eventRuleCache.getEventRuleByEventTypeId(id);
        if (list == null || list.size() == 0) {
            if (eventTypeCache.get(id) != null) {
                eventTypeService.delete(id);
                eventTypeCache.remove(id);
                renderText("true");
            }
        } else {
            renderText("false");
        }
        return null;
    }

    public String checkDeleted() {
        JSONObject ob = new JSONObject();
        if (eventTypeCache.get(id) == null) {
            ob.element("flag", false);
        } else {
            ob.element("flag", true);
        }
        Struts2Utils.renderJson(ob.toString());
        return null;
    }

    @Override
    protected void prepareModel() throws Exception {
        if (id != null) {
            entity = eventTypeCache.get(id);
            if (entity == null) {
                entity = new EventType();
            }
        } else {
            entity = new EventType();
        }
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}
